package com.amos.knife4j.controller;

import com.amos.knife4j.config.repetition.RepetitionSubmit;
import com.amos.knife4j.config.result.ResultEnums;
import com.amos.knife4j.config.result.Result;
import com.amos.knife4j.config.result.ResultUtil;

import com.amos.knife4j.config.exception.BusinessException;
import com.amos.knife4j.config.exception.controller.ControllerExceptionHandler;

import com.amos.knife4j.dto.DicBean;
import com.amos.knife4j.dto.form.DicForm;
import com.amos.knife4j.service.DicService;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

/** 
 * <br/>
 * Created by amos on 2021/09/17
 */


@Api(tags = "")
@ApiSupport(author = "amos", order = 1)
@RestController
@RequestMapping("/dic")
public class DicController extends AbstractController {
    // 注入Service
    @Autowired
    private DicService dicService;


    /**
     * 新增Dic
     *
     * @author amos
     * @date 2021-09-17 10:25:08
     */


    @ApiImplicitParams({

                      @ApiImplicitParam(name = "code", value = "字典值", paramType = "body", required = true,  dataType = "string"),
                        @ApiImplicitParam(name = "name", value = "字典名称", paramType = "body", required = true,  dataType = "string"),
                        @ApiImplicitParam(name = "parentId", value = "父级字典", paramType = "body", required = true,  dataType = "string"),
                        @ApiImplicitParam(name = "state", value = "字典状态，是否启用，0：禁用，1：启用", paramType = "body", required = true,  dataType = "string"),
                        @ApiImplicitParam(name = "sort", value = "排序", paramType = "body", required = true,  dataType = "int"),
                        @ApiImplicitParam(name = "ticket", value = "用户token", paramType = "header", required = true, dataType = "string")
         
    })
    @ApiOperation(value = "新增")
    @PostMapping("/v1/info")
    @RepetitionSubmit
    @ControllerExceptionHandler
    public Result<String> insert(@Valid DicForm form, BindingResult bindingResult, HttpServletRequest request) {
        // 表单验证
        if (bindingResult.hasErrors()) {
            // 错误信息包装成业务异常抛出
            throw new BusinessException(ResultEnums.FAILED.getCode(), bindingResult.getFieldError().getDefaultMessage());
        }
        String userId = getUserId(request);
        dicService.insert(userId, form);
        return ResultUtil.success(ResultEnums.SUCC_ADD);
    }

    /**
     * 修改Dic
     *
     * @author amos
     * @date 2021-09-17 10:25:08
     */

    @ApiImplicitParams({

                  @ApiImplicitParam(name = "id", value = "主键id", paramType = "path", required = true, dataType = "string"),
                    @ApiImplicitParam(name = "code", value = "字典值", paramType = "body", required = false,  dataType = "string"),
                    @ApiImplicitParam(name = "name", value = "字典名称", paramType = "body", required = false,  dataType = "string"),
                    @ApiImplicitParam(name = "parentId", value = "父级字典", paramType = "body", required = false,  dataType = "string"),
                    @ApiImplicitParam(name = "state", value = "字典状态，是否启用，0：禁用，1：启用", paramType = "body", required = false,  dataType = "string"),
                    @ApiImplicitParam(name = "sort", value = "排序", paramType = "body", required = false,  dataType = "int"),
                    @ApiImplicitParam(name = "ticket", value = "用户token", paramType = "header", required = true, dataType = "string"),
                 })

    @ApiOperation(value = "修改")
    @PutMapping("/v1/info/{id}")
    @ControllerExceptionHandler
    public Result<String> update(@PathVariable("id") String id,DicForm form, HttpServletRequest request) {
        String userId = getUserId(request);
        dicService.update(id,userId, form);
        return ResultUtil.success(ResultEnums.SUCC_UPDATE);
    }

    /**
     * 状态停用启用
     *
     * @author amos
     * @date 2021-09-17 10:25:08
     */
   @ApiImplicitParams({
                @ApiImplicitParam(name = "ticket", value = "用户token", paramType = "header", required = true, dataType = "string"),
                @ApiImplicitParam(name = "id", value = "主键id", paramType = "path", required = true, dataType = "string"),
                @ApiImplicitParam(name = "state", value = "状态0停用1启用",paramType = "query", required = true,dataType = "string")
   })
   @ApiOperation(value = "状态启用停用")
    @PutMapping("/v1/info/{id}/state")
    @ControllerExceptionHandler
    public Result<String> updateState(@PathVariable("id") String id,
                                        @RequestParam String state,
                                        HttpServletRequest request) {
        String userId = getUserId(request);
        dicService.updateState(id,userId,state);
        return ResultUtil.success(ResultEnums.SUCC_UPDATE);
    }

    /**
     * 删除Dic
     *
     * @author amos
     * @date 2021-09-17 10:25:08
     */
      @ApiImplicitParams({
                     @ApiImplicitParam(name = "ticket", value = "用户token", paramType = "header", required = true, dataType = "string"),
                     @ApiImplicitParam(name = "id", value = "主键id", paramType = "path", required = true, dataType = "string")
        })
        @ApiOperation(value = "删除")
    @DeleteMapping("/v1/info/{id}")
    @ControllerExceptionHandler
    public Result<String> delete(@PathVariable("id") String id, HttpServletRequest request) {
        String userId = getUserId(request);
        dicService.delete(id);
        return ResultUtil.success(ResultEnums.SUCC_DELETE);
    }

    /**
     * 分页查询Dic
     *
     * @author amos
     * @date 2021-09-17 10:25:08
     */
    @ApiImplicitParams({
             @ApiImplicitParam(name = "ticket", value = "用户token", paramType = "header", required = true, dataType = "string"),
             @ApiImplicitParam(name = "pageSize", value = "每页显示条数", paramType = "path", required = true, dataType = "int"),
             @ApiImplicitParam(name = "pageNum", value = "页码",paramType = "path", required = true,  dataType = "int")
    })
    @ApiOperation(value = "分页列表")
    @GetMapping("/v1/info/{pageSize}/{pageNum}")
    @ControllerExceptionHandler
    public Result<PageInfo<DicBean>> selectDicPage(
            @PathVariable("pageSize") int pageSize,
            @PathVariable("pageNum") int pageNum) {
        // 查询
        PageInfo<DicBean> pages = dicService.selectDicPage(pageNum, pageSize);
        // 返回查询结果
        return ResultUtil.success(pages);
    }

    /**
     * Dic详情
     *
     * @author amos
     * @date 2021-09-17 10:25:08
     */
      @ApiImplicitParams({
                          @ApiImplicitParam(name = "ticket", value = "用户token", paramType = "header", required = true, dataType = "string"),
                          @ApiImplicitParam(name = "id", value = "主键id", paramType = "path", required = true, dataType = "string")
             })
             @ApiOperation(value = "详情")
    @GetMapping("/v1/info/{id}")
    @ControllerExceptionHandler
    public Result<DicBean> selectDicDetail(
            @PathVariable String id
    ) {
        // 查询
        DicBean dicBean = dicService.selectDicDetail(id);
        // 返回查询结果
        return ResultUtil.success(dicBean);
    }
}